package com.jakewharton.rxbinding4.component;

import static com.jakewharton.rxbinding4.internal.Preconditions.checkMainThread;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.openharmony.MainThreadDisposable;
import ohos.agp.components.PageSlider;

final class PageSliderPageSlideStateChangedObservable extends Observable<Integer> {
    private final PageSlider view;

    PageSliderPageSlideStateChangedObservable(PageSlider view) {
        this.view = view;
    }

    @Override
    protected void subscribeActual(Observer<? super Integer> observer) {
        if (!checkMainThread(observer)) {
            return;
        }
        Listener listener = new Listener(view, observer);
        observer.onSubscribe(listener);
        view.addPageChangedListener(listener);
    }

    static final class Listener extends MainThreadDisposable implements PageSlider.PageChangedListener {
        private final PageSlider view;
        private final Observer<? super Integer> observer;

        Listener(PageSlider view, Observer<? super Integer> observer) {
            this.view = view;
            this.observer = observer;
        }

        @Override
        protected void onDispose() {
            view.removePageChangedListener(this);
        }

        @Override
        public void onPageSliding(int position, float positionOffset, int positionOffsetPixels) {}

        @Override
        public void onPageSlideStateChanged(int state) {
            if (!isDisposed()) {
                observer.onNext(state);
            }
        }

        @Override
        public void onPageChosen(int i) {}
    }
}
